var express = require('express');
var router = express.Router();
var article = require('../db/article.js');
var User = require('../db/user.js');
var tools = require('../units/tools.js');


//移动文章
router.post("/changeArr", function (req, res, next) {
    article.updateOne({ a_id: req.body.aid }, { fileName: req.body.filename }, function (err) {
        if (err) { console.log(err); }
        res.send("移动成功");
    });
});

//获取整理的文件夹
router.post("/getArrange", function (req, res, next) {
    User.findOne({ uid: req.cookies.userID }, function (err, list) {
        if (err) { console.log(err); }
        res.send(list.arrange);
    })
});

//新建整理的文件夹
router.post("/doArrange", function (req, res, next) {
    User.updateOne({ uid: req.cookies.userID }, { $addToSet: { arrange: { fileName: req.body.filename } } }, function (err) {
        if (err) { console.log(err); }
        res.json({
            data: "创建成功",
            filename: req.body.filename,
        });
    })
});

//文章里的图片上传
router.post("/imgArticle", tools.multer().single("PicturesOfAs"), function (req, res, next) {
    // console.log(req.file);
    // console.log(req.body);
    let imgD = req.file.path.split("\\");
    let imgUrl = "http://localhost:3000/" + imgD[1] + "/" + imgD[2] + "/" + imgD[3];
    article.findOne({ a_id: req.body.aid }, function (err, data) {
        if (err) { console.log(err); }
        if (data.coverImg == null) {
            article.updateOne({ a_id: req.body.aid }, { coverImg: imgUrl }, function (err) {
                if (err) { console.log(err); }
            })
        }
    })
    res.json({
        "errno": 0,
        "data": [{
            url: imgUrl,
            alt: "PicturesOfAs"
        }],
    });
});

//发布文章
router.post("/openArticle", function (req, res, next) {
    let aid = req.body.aid;
    let uptime = req.body.addTime;
    let Astitle = req.body.title;
    let intrs = req.body.intr;
    let Ascontent = req.body.content;
    article.updateOne({ a_id: aid }, { addTime: uptime, title: Astitle, content: Ascontent, intr: intrs, a_type: true }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            res.send("发布成功");
        }
    });
});

//保存文章
router.post("/doArticle", function (req, res, next) {
    let aid = req.body.aid;
    let Astitle = req.body.title;
    let intrs = req.body.intr;
    let Ascontent = req.body.content;
    article.updateOne({ a_id: aid }, { title: Astitle, content: Ascontent, intr: intrs }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            res.send("保存成功");
        }
    });
});

//返回文章信息
router.post("/getArticle", function (req, res, next) {
    let aid = req.body.aid;
    article.findOne({ a_id: aid }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            res.send({
                title: data.title,
                content: data.content,
                intr: data.intr,
            });
        }
    });
});

//删除文章
router.post("/deleteArticle", function (req, res, next) {
    let aid = req.body.aid;
    article.deleteOne({ a_id: aid }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            // console.log(data);
            res.send("删除成功");
        }
    })
});

//返回文章列表信息
router.post("/getArticleList", function (req, res, next) {
    article.find({ uid: req.cookies.userID }, function (err, data) {
        if (err) { console.log(err); return; }
        else {
            // console.log(data);
            User.findOne({ uid: req.cookies.userID }, function (err, item) {
                if (err) { console.log(err); return; }
                let AsList = [];
                for (let i = data.length - 1; i >= 0; i--) {
                    AsList.push({
                        aid: data[i].a_id,
                        title: data[i].title,
                        type: data[i].a_type,
                        filename: data[i].fileName,
                    })
                };
                res.json({
                    AsList: AsList,
                    filename: item.arrange,
                });
            })
        }
    });
});

//创建文章
router.post("/putArticleList", function (req, res, next) {
    let Astitle = req.body.title;

    User.findOne({ uid: req.cookies.userID }, function (err, item) {
        if (err) { console.log(err); return; }
        let As = article({
            uid: req.cookies.userID,
            uname: item.uname,
            a_id: "As/" + req.cookies.userID + "/" + Astitle,
            title: Astitle,
            a_type: false,
            intr: "",
            viewCount: 0,
            fileName: "未分类"
        });
        As.save(function (err) {
            if (err) {
                console.log(err);
            } else {
                // console.log(datas);
                res.send("新建文章成功");
            }
        });
    })

});

module.exports = router